package com.wteam.dragon.shop.service.impl;

import cn.hutool.core.lang.Dict;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wteam.dragon.shop.mapper.ProductMapper;
import com.wteam.dragon.shop.mapper.ProductSortDtoMapper;
import com.wteam.dragon.shop.mapper.ProductSortMapper;
import com.wteam.dragon.shop.pojo.bean.Product;
import com.wteam.dragon.shop.pojo.bean.ProductSort;
import com.wteam.dragon.shop.pojo.dto.ProductSortDto;
import com.wteam.dragon.shop.service.ProductSortService;
import com.wteam.dragon.system.basebean.bean.ResultMessage;
import com.wteam.dragon.system.exception.BadRequestException;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author 陈晓辉
 * @Date 2020/7/2 11:12
 */
@Service
@RequiredArgsConstructor
public class ProductSortServiceImpl implements ProductSortService {
    private final ProductSortMapper productSortMapper;
    private final ProductSortDtoMapper productSortDtoMapper;
    private final ProductMapper productMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage addProductSort(ProductSort productSort) {
        productSortMapper.insert(productSort);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("添加成功");
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage delProductSort(Long id) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(Product::getProductSortId, id);
        Integer count = productMapper.selectCount(queryWrapper);
        if(count != 0){
            throw new BadRequestException("该类型有商品关联，不能进行删除");
        }
        productSortMapper.deleteById(id);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("删除成功");
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage editProductSort(ProductSort productSort) {
        productSortMapper.updateById(productSort);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("编辑成功");
        return resultMessage;

    }

    @Override
    public ResultMessage listParent() {
        QueryWrapper<ProductSort> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(ProductSort::getParentId, 0)
                .orderByAsc(ProductSort::getSort);
        List<ProductSort> productSortList = productSortMapper.selectList(queryWrapper);
        ResultMessage resultMessage = new ResultMessage();
        Dict resultMap = Dict.create()
                .set("message", "请求成功")
                .set("productSortList", productSortList);
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(resultMap);
        return resultMessage;
    }

    @Override
    public ResultMessage listSon(Long id) {
        QueryWrapper<ProductSort> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(ProductSort::getParentId, id)
                .orderByAsc(ProductSort::getSort);
        List<ProductSort> productSortList = productSortMapper.selectList(queryWrapper);
        ResultMessage resultMessage = new ResultMessage();
        Dict resultMap = Dict.create()
                .set("message", "请求成功")
                .set("productSortList", productSortList);
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(resultMap);
        return resultMessage;
    }

    @Override
    public ResultMessage getProductSort(Long id) {
        ProductSort productSort = productSortMapper.selectById(id);
        ResultMessage resultMessage = new ResultMessage();
        Dict resultMap = Dict.create()
                .set("message", "请求成功")
                .set("productSort", productSort);
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(resultMap);
        return resultMessage;
    }

    @Override
    public ResultMessage getBar() {
        QueryWrapper<ProductSortDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(ProductSortDto::getNavStatus, 1)
                .eq(ProductSortDto::getShowStatus, 1)
                .eq(ProductSortDto::getParentId, 0)
                .orderByAsc(ProductSortDto::getSort);
        List<ProductSortDto> productSortDtoList = productSortDtoMapper.selectList(queryWrapper);
        productSortDtoList.forEach(productSortDto -> {
            QueryWrapper<ProductSort> productSortQueryWrapper = new QueryWrapper<>();
            productSortQueryWrapper.lambda()
                    .eq(ProductSort::getParentId, productSortDto.getId());
            productSortDto.setSonList(productSortMapper.selectList(productSortQueryWrapper));
        });
        ResultMessage resultMessage = new ResultMessage();
        Dict resultMap = Dict.create()
                .set("message", "请求成功")
                .set("productSortDtoList", productSortDtoList);
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(resultMap);
        return resultMessage;
    }
}
